package com.javaranger.news.dao;

import java.util.Map;

import org.apache.log4j.Logger;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.stereotype.Component;

import com.google.common.collect.Maps;
import com.javaranger.news.pojo.Message;
import com.javaranger.news.service.QueryService;

@Component
public class MessageMapper extends SqlSessionDaoSupport {
	private final Logger log = Logger.getLogger(MessageMapper.class);

	public boolean saveMessage(Message message) {
		return getSqlSession().insert("saveMessage", message) > 0;
	}
	
	public Message getMessage(String type,String q){
		Map<String, Object> parameter = Maps.newHashMap();
		if (QueryService.ID.equals(type)) {
			parameter.put("code", q);
			return getSqlSession().selectOne("getIdMessage", parameter);
		}else if (QueryService.IP.equals(type)) {
			parameter.put("ip", q);
			return getSqlSession().selectOne("getIpMessage", parameter);
		}else if (QueryService.MOBILE.equals(type)) {
			parameter.put("phonenum", q);
			return getSqlSession().selectOne("getMobileMessage", parameter);
		}
		return null;
	}
}
